package com.nerve.cloudoffice.web.action;

import com.nerve.cloudoffice.common.Reply;
import com.nerve.cloudoffice.service.UserService;
import com.nerve.cloudoffice.service.impl.UserServiceImpl;
import com.nerve.cloudoffice.web.model.UserModel;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @project: cloudOffice
 * @file: UserAction.java
 * @package: com.nerve.cloudoffice.web.action
 * @description:
 *	用户action
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: 2013-8-2
 * @change log:
 */
@SuppressWarnings("serial")
public class UserAction extends NerveAction implements ModelDriven<UserModel>{
	private UserService service = new UserServiceImpl();
	
	private UserModel model = new UserModel();
	
	/**
	 * 新增一个职员
	 */
	public void add(){
		Reply reply = new Reply();
		try{
			service.save(model);
			reply.setMessage(model.getUser().getId());
		}catch(Exception e){
			error(e.getMessage(), e);
			System.err.println(e.getMessage());
			
			reply.error(e);
		}
		
		json(reply);
	}
	
	
	/**
	 * 更新一个职员信息
	 * 目前只提供以下的更新：
	 * 	0	更新密码
	 * 	1	更改职务
	 * 	2	更改职员的有效性
	 * 	3	更改职员所在部门
	 * 
	 * updateType = 0/1/2/3  表示更新类型
	 * updateValue 为更新的内容
	 */
	public void update(){
		Reply reply = new Reply();
		try{
			service.update(model);
			
			reply.setMessage(SUCCESS);
		}catch(Exception e){
			error(e.getMessage(), e);
			reply.setError(true);
			reply.setMessage(e.getMessage());
			
			e.printStackTrace();
		}
		
		json(reply);
	}
	
	public void updateBatch(){
		Reply reply = new Reply();
		try{
			service.updateBatch(model);
			
			reply.setMessage(SUCCESS);
		}catch(Exception e){
			error(e.getMessage(), e);
			reply.setError(true);
			reply.setMessage(e.getMessage());
		}
		
		json(reply);
	}
	
	/**
	 * 获取职员的列表
	 */
	public void list(){
		Reply reply = new Reply();
		try{
			service.list(model);
			
			reply.setMessage(model.getUserList());
			reply.setData(model.getTotalCount());
		}catch(Exception e){
			error(e.getMessage(), e);
			reply.setError(true);
			reply.setMessage(e.getMessage());
		}
		
		json(reply);
	}

	

	public void setModel(UserModel model) {
		this.model = model;
	}

	public UserModel getModel() {
		return model;
	}
}